package com.example.patentback.service.impl;

import com.example.patentback.dao.ApplicantMapper;
import com.example.patentback.entity.Applicant;
import com.example.patentback.service.ApplicantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApplicantServiceImpl implements ApplicantService {

    @Autowired
    ApplicantMapper applicantMapper;

    @Override
    public Applicant selectById(Long id) {
        Example example = new Example(Applicant.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        return applicantMapper.selectByExample(example).get(0);
    }

    @Override
    public List<Long> addApplicants(List<Applicant> applicants) {
        applicantMapper.insertList(applicants);
        List<Long> collect = applicants.stream().map(applicant -> applicant.getId()).collect(Collectors.toList());
        return collect;
    }

    @Override
    public int deleteApplicants(Long[] applicantIds) {
        int i = 0;
        for (Long applicantId : applicantIds) {
            Example example = new Example(Applicant.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id", applicantId);
            i += applicantMapper.deleteByExample(example);
        }
        return i;
    }

    @Override
    public Applicant selectByUserId(Long userId) {
        Example example = new Example(Applicant.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        return applicantMapper.selectByExample(example).get(0);
    }

    @Override
    public List<Applicant> selectByIds(List<Long> ids) {
        Example example = new Example(Applicant.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return applicantMapper.selectByExample(example);
    }
}
